using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace Skinner
{
    public partial class ColorPalette : UserControl
    {
        private ColorPalette()
        {
            InitializeComponent();
        }

        private StyleSheet _sheet = null;

        public ColorPalette(StyleSheet sheet)
        {
            InitializeComponent();
            _sheet = sheet;

            int i = 0;
            foreach (KeyValuePair<string, ColorRule> c in _sheet.Colors)
            {
                AddColorControls(i++, c.Key);
            }
            this.Height = i * 38 + 38;
        }

        private void AddColorControls(int position, string key)
        {
            int height = 33;
            int width = 99;
            int topPad = 5;
            int leftPad = 5;
            int topOffset = 13;
            int leftOffset = 5;

            ColorRule color = _sheet.Colors[key];

            Button btnOriginal = new Button();
            Button btnNew = new Button();
            Label styleCount = new Label();             

            btnOriginal.Name = string.Format("Color{0}", position);

            try
            {
                btnOriginal.Location = new Point(leftPad, ((topPad * position) == 0 ? topPad + topOffset : (topPad * (position + 1)) + (height * position) + topOffset));
                btnOriginal.Size = new Size(width, height);
                btnOriginal.FlatStyle = FlatStyle.Flat;
                
                //Yes, it's lame! Need to write parser for names.
                try
                {
                    btnOriginal.BackColor = System.Drawing.Color.FromArgb(int.Parse(color.ColorName.Substring(1, 2), System.Globalization.NumberStyles.HexNumber), int.Parse(color.ColorName.Substring(3, 2), System.Globalization.NumberStyles.HexNumber), int.Parse(color.ColorName.Substring(5, 2), System.Globalization.NumberStyles.HexNumber));
                }
                catch 
                {
                    try
                    {
                        btnOriginal.BackColor = System.Drawing.Color.FromName(color.ColorName);
                    }
                    catch { }
                }
                btnOriginal.Text = color.ColorName;
                btnOriginal.Tag = btnNew;
                btnOriginal.Click += new EventHandler(btnOriginal_Click);
                Controls.Add(btnOriginal);

                btnNew.Location = new Point(leftPad + width + leftOffset, ((topPad * position) == 0 ? topPad + topOffset : (topPad * (position + 1)) + (height * position) + topOffset));
                btnNew.Size = new Size(width, height);
                btnNew.FlatStyle = FlatStyle.Flat;
                try
                {
                    btnNew.BackColor = System.Drawing.Color.FromArgb(int.Parse(color.ColorName.Substring(1, 2), System.Globalization.NumberStyles.HexNumber), int.Parse(color.ColorName.Substring(3, 2), System.Globalization.NumberStyles.HexNumber), int.Parse(color.ColorName.Substring(5, 2), System.Globalization.NumberStyles.HexNumber));
                }
                catch
                {
                    try
                    {
                        btnNew.BackColor = System.Drawing.Color.FromName(color.ColorName);
                    }
                    catch { }                    
                }
                btnNew.Text = color.ColorName;
                btnNew.Tag = key;
                btnNew.Click += new EventHandler(btnNew_Click);
                Controls.Add(btnNew);

                styleCount.Location = new Point(leftPad + width * 2 + leftOffset, ((topPad * position) == 0 ? topPad + topOffset : (topPad * (position + 1)) + (height * position) + topOffset) + 8);
                styleCount.Size = new Size(width, height);
                styleCount.Text = color.RuleSelectors.Count.ToString();
                styleCount.TextAlign = System.Drawing.ContentAlignment.TopCenter;
                Controls.Add(styleCount);
            }
            catch { }
        }

        private void btnOriginal_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Revert all styles to this color?", "Undo Changes?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                //ok, it's a silly way to do it. REFACTOR the whole key question.
                string key = (string)((Button)((Button)sender).Tag).Tag;
                ColorRule colorRule = _sheet.Colors[key];
                try
                {
                    foreach (string ruleName in colorRule.RuleSelectors)
                    {
                        StyleRule rule = _sheet.Rules[ruleName.ToUpper()];

                        foreach (mshtml.IHTMLStyleSheetRule innerRule in rule.Rules)
                        {
                            innerRule.style.cssText = innerRule.style.cssText.ToLower().Replace(colorRule.NewColorName.ToLower(), colorRule.ColorName.ToLower());
                        }
                    }

                    colorRule.NewColorName = colorRule.ColorName;

                    ((Button)((Button)sender).Tag).BackColor = ((Button)sender).BackColor;
                    ((Button)((Button)sender).Tag).Text = colorRule.NewColorName;

                }
                catch { }
            }
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            string key = (string)((Button)sender).Tag;
            ColorEdit editForm = new ColorEdit(_sheet, _sheet.Colors[key], (Button)sender);
            editForm.Show();
        }
    }
}
